//
//  YDLiveViewController.m
//  yidianyuanqicelue
//
//  Created by LK-IOS on 2018/6/27.
//  Copyright © 2018年 LK-YDIOS. All rights reserved.
//

#import "YDLiveViewController.h"
#import <WebKit/WebKit.h>


@interface YDLiveViewController ()<WKNavigationDelegate,WKUIDelegate>


@property (nonatomic , weak)UIProgressView *progressView;
@property (nonatomic , weak) WKWebView *webView;

@end

@implementation YDLiveViewController

-(void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = NumberColor(247, 247, 247);
    self.title = @"直播";
    [self progressView];
    [self addWebView];
    
}

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
//    [self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
    
}
- (UIProgressView *)progressView
{
    if (!_progressView){
        UIProgressView *progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 0, LKScreenW, 0)];
        progressView.tintColor = [UIColor colorWithHexString:@"FBB03B"];
        progressView.trackTintColor = [UIColor colorWithHexString:@"FBB03B"];
        [self.view addSubview:progressView];
        self.progressView = progressView;
    }
    return _progressView;
}

-(void)addWebView
{
    WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.bounds];
    webView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    webView.backgroundColor = [UIColor whiteColor];
    webView.navigationDelegate = self;
    
    /*! 适应屏幕 */
    //        wkWebView.scalesPageToFit = YES;
    /*! 解决iOS9.2以上黑边问题 */
    webView.opaque = NO;
    /*! 关闭多点触控 */
    webView.multipleTouchEnabled = YES;
    /*! 加载网页中的电话号码，单击可以拨打 */
    //        wkWebView.dataDetectorTypes = YES;
    
    [self.view insertSubview:webView belowSubview:_progressView];
    
    [webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
    
    NSURL *baseURL = [NSURL URLWithString:@"http://y.zhongyuguozi.com/"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:baseURL];
    
    [webView loadRequest:request];
    
    self.webView = webView;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if (object == self.webView && [keyPath isEqualToString:@"estimatedProgress"])
    {
        CGFloat newprogress = [[change objectForKey:NSKeyValueChangeNewKey] doubleValue];
        if (newprogress == 1)
        {
            self.progressView.hidden = YES;
            [self.progressView setProgress:0 animated:NO];
        }
        else
        {
            self.progressView.hidden = NO;
            [self.progressView setProgress:newprogress animated:YES];
        }
    }
}

@end
